
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

// -------------------------------------------------------------
// Mesh Cloth Maker Sphere - v0.7
// Made By: Suicidal.Banana
// Status: works
// -------------------------------------------------------------
// Disclaimer: You are free to use bits of code from my sent, 
// as long as you GIVE ME CREDIT, Suicidal.Banana
// -------------------------------------------------------------


/*---------------------------------------------------------
   Funcions: Create
---------------------------------------------------------*/
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create( "MeshCloth_Sphere" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
	
end

function ENT:SetMeshData( pos1, pos2, pos3, pos4, meshheight, showmesh, meshmat )
		self.Entity:SetNetworkedInt("pos1",pos1)
		self.Entity:SetNetworkedInt("pos2",pos2)
		self.Entity:SetNetworkedInt("pos3",pos3)
		self.Entity:SetNetworkedInt("pos4",pos4)
		self.Entity:SetNetworkedInt("meshheight",meshheight)
		self.Entity:SetNetworkedInt("showmesh",showmesh)
		self.Entity:SetNetworkedString("meshmat",meshmat)
end



function ENT:KeyValue (key, value)

    self[key] = tonumber(value) or value
    
end 

/*---------------------------------------------------------
   Funcions: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
	
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:SetModel("models/props/cs_italy/orange.mdl") 
	// Don't use the model's physics - create a sphere instead
	self.Entity:PhysicsInitSphere( 4, "metal_bouncy" )
	
	// Wake the physics object up. It's time to have fun!
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableMotion(false)
	end
	
	// Set collision bounds exactly
	self.Entity:SetCollisionBounds( Vector( -2, -2, -2 ), Vector( 2, 2, 2 ) )
	self.Entity:SetFriction(10)
	self.Entity:DrawShadow(false)
	self.Entity:SetColor( 0, 100, 255, 100 )
	
	
	local plyid = self.theplyid
	local selfcountx = self.countx
	local selfcounty = self.county
	local ropelength 	= self.ropeleng
	local clothcount 	= self.theclothcount
	local showtherope 	= self.showrope
	
	local ropewidth 	= showtherope
	local ropematerial 	= "cable/rope"
	local LPos1 = Vector( 0, 0, 0 )
		
	if selfcountx > 0 then
	local xsphere_name = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. selfcountx - 1 .. "_y" .. selfcounty
	local xsphere_check = ents.FindByName( xsphere_name )
	for ropecountx, xsphere in pairs(xsphere_check) do 
		constraint.Rope( self.Entity, xsphere, 0, 0, LPos1, LPos1, 0, ropelength, 0, ropewidth, ropematerial, 1 )
	end
	end
	
	if selfcounty > 0 then
	local ysphere_name = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. selfcountx .. "_y" .. selfcounty - 1 
	local ysphere_check = ents.FindByName( ysphere_name )
	for ropecounty, ysphere in pairs(ysphere_check) do 
		constraint.Rope( self.Entity, ysphere, 0, 0, LPos1, LPos1, 0, ropelength, 0, ropewidth, ropematerial, 1 )
	end
	end	
end



/*---------------------------------------------------------
   Funcions: Collisions
---------------------------------------------------------*/

// Bounce
function ENT:PhysicsCollide( data, physobj )

	local phys = self.Entity:GetPhysicsObject()
	local LastSpeed = data.OurOldVelocity:Length()
	local NewVelocity = physobj:GetVelocity()
	NewVelocity:Normalize()
	
	LastSpeed = math.max( NewVelocity:Length(), LastSpeed )
	
	local TargetVelocity = NewVelocity * LastSpeed * 0.1
	
	physobj:SetVelocity( TargetVelocity )
	
end

/*---------------------------------------------------------
   Funcions: OnTakeDamage and Touch
---------------------------------------------------------*/
// Take physics damage
function ENT:OnTakeDamage()
	self.Entity:TakePhysicsDamage( dmginfo )
end


function ENT:Touch(ent)

end

/*---------------------------------------------------------
   Funcions: Use and Think
---------------------------------------------------------*/
function ENT:Use( activator, caller )
	
end

function ENT:Think()

end


